package com.mkbk.controller;

import com.google.gson.Gson;
import com.mkbk.model.Supply;
import com.mkbk.service.SupplyService;
import com.mkbk.service.impl.SupplyServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/supply")
public class SupplyServlet extends HttpServlet {
    private final SupplyService supplyService = new SupplyServiceImpl();
    private static final Gson gson = new Gson();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        super.service(req, res);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        handleRequest(req, res);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        handleRequest(req, res);
    }

    private void handleRequest(HttpServletRequest req, HttpServletResponse res) throws IOException {
        req.setCharacterEncoding("utf-8");
        String optionType = req.getParameter("optionType");
        res.setContentType("application/text;charset=utf-8");
        // 设置跨域
        res.setHeader("Access-Control-Allow-Origin", "*");
        String response;

        if ("search".equals(optionType)) {
            response = handleSearch(req);
        } else if ("page".equals(optionType)) {
            response = handlePage(req);
        } else if ("add".equals(optionType)) {
            response = handleAdd(req);
        } else if ("delete".equals(optionType)) {
            response = handleDelete(req);
        } else if ("update".equals(optionType)) {
            response = handleUpdate(req);
        } else {
            response = "{\"status\":\"Invalid optionType\"}";
        }

        res.getWriter().write(response);
    }

    private String handleSearch(HttpServletRequest req) {
        Supply supply = new Supply();
        supply.setsName(req.getParameter("sName"));
        List<Supply> supplyList = supplyService.getAllSupply(supply);
        return gson.toJson(supplyList);
    }

    private String handlePage(HttpServletRequest req) {
        Supply supply = new Supply();
        int current = Integer.parseInt(req.getParameter("current"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        List<Supply> supplyList = supplyService.getAllSupplyByPage(supply, current, pageSize);
        return gson.toJson(supplyList);
    }

    private String handleAdd(HttpServletRequest req) {
        Supply supply = new Supply();
        supply.setsName(req.getParameter("sName"));
        supply.setAddress(req.getParameter("address"));
        supply.setPhone(req.getParameter("phone"));
        supplyService.addSupply(supply);
        return "{\"status\":\"添加成功\"}";
    }

    private String handleDelete(HttpServletRequest req) {
        supplyService.deleteSupplyById(Integer.parseInt(req.getParameter("id")));
        return "{\"status\":\"删除成功\"}";
    }

    private String handleUpdate(HttpServletRequest req) {
        Supply supply = new Supply();
        supply.setId(Integer.parseInt(req.getParameter("id")));
        supply.setsName(req.getParameter("sName"));
        supply.setAddress(req.getParameter("address"));
        supply.setPhone(req.getParameter("phone"));
        supplyService.updateSupplyById(supply);
        return "{\"status\":\"修改成功\"}";
    }
}